package com.garmin.tw.garminpcsync.android;

import java.net.URI;

import org.xmlrpc.android.XMLRPCClient;
import org.xmlrpc.android.XMLRPCMethod;
import org.xmlrpc.android.XMLRPCMethodCallback;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.garmin.tw.garminpcsync.android.sessions.SessionManager;
import com.garmin.tw.garminpcsync.common.domain.xmlrpc.AuthResult;
import com.google.analytics.tracking.android.EasyTracker;
import com.google.analytics.tracking.android.Tracker;

public class LoginActivity extends Activity {

	private XMLRPCClient client;
	
	private URI uri;
	
	private ProgressDialog progressDialog;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.auth);
		
		// Get current context.
		Context mCtx = this.getApplicationContext();
		// setContext will use mCtx to retrieve the application context.
		EasyTracker.getInstance().setContext(mCtx);
		// EasyTracker is now ready for use.
		final Tracker myTracker = EasyTracker.getTracker();
		
		uri = URI.create("http://my.garmin.com.tw/GarminPCSync/xmlrpc");
		client = new XMLRPCClient(uri);
		
		final EditText editText1 = (EditText)this.findViewById(R.id.editText1);
		//editText1.setText("duncan1027");
		editText1.setText("tvupdater");
		
		final EditText editText2 = (EditText)this.findViewById(R.id.editText2);
		//editText2.setText("duncan1027");
		editText2.setText("tvupdater");
		
		final EditText editText3 = (EditText)this.findViewById(R.id.editText3);
		//editText3.setText("1397719729");
		//editText3.setText("3829930355");
		editText3.setText("3849606838");
		
		Button button1 = (Button)this.findViewById(R.id.button1);
		button1.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				
				myTracker.trackEvent("Login Page", "Garmin Account Login", null, null);
				
				XMLRPCMethod method = new XMLRPCMethod(client, "APIDispatcher.auth",
						new XMLRPCMethodCallback() {
							public void callFinished(Object result) {
								
								if (result instanceof AuthResult) {
									AuthResult authResult = (AuthResult)result;
									
									if (authResult.getReturnCode().intValue() == 1) {
										SessionManager sm = new SessionManager(LoginActivity.this);
										sm.createLoginSession(editText1.getText().toString(), editText2.getText().toString(), authResult.getEmail(), editText3.getText().toString());
										
										/*Intent intent = new Intent(LoginActivity.this, MainEntry.class);
										intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
										intent.setFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
										startActivity(intent);*/
										finish();
									}
								}
								
								progressDialog.cancel();
							}
						});
				
				progressDialog = new ProgressDialog(LoginActivity.this);
				progressDialog.setMessage("Loading. Please wait...");
				progressDialog.setCancelable(false);
				progressDialog.show();
				
				/* userId
				 * password
				 * esn
				 */
				Object[] params = { editText1.getText().toString(), editText2.getText().toString(), editText3.getText().toString() };
				method.call(params);
			}
			
		});
		
		Button button2 = (Button)this.findViewById(R.id.button2);
		button2.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				/*Intent intent = new Intent(LoginActivity.this, MainEntry.class);
				intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				intent.setFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
				startActivity(intent);*/
				finish();
			}
			
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		//getMenuInflater().inflate(R.menu.login, menu);
		
		return true;
	}

}
